﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Yugi.Client.ViewModel
{
    public class ListOnlineModel
    {
        public ObservableCollection<UserModel> listonline;

        public ListOnlineModel()
        {
            listonline = new ObservableCollection<UserModel>();
            listonline.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(listonline_CollectionChanged);
        }

        void listonline_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {

            }
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {

            }
        }

        public void AddUser(string n)
        {
            listonline.Add(new UserModel(n));
        }

        public void RemoveUser(string n)
        {
            foreach (UserModel user in listonline)
            {
                if (user.name == n)
                {
                    listonline.Remove(user);
                    break;
                }
            }
        }

        public void ReceiveMSG(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.User_On:
                    AddUser((string)obj[0]);
                    break;
                case ClientMSG.User_Off:
                    RemoveUser((string)obj[0]);
                    break;
                case ClientMSG.User_AllOn:
                    foreach (object o in obj)
                    {
                        AddUser((string)o);
                    }
                    break;
            }
        }
    }
}
